﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PPIDesktopClient.Entities;
using VMind.Global;

namespace PPIDesktopClient.Controls.Grid
{
    public partial class AssetGrid : UserControl
    {
        public List<EUtility_QueryAssetShown> QueryAssetsToShown { get; set; }
        protected EUtility_QueryAssetShown m_CurrentQueryWorker;
        protected List<int> m_AlredyStructuresLoad = new List<int>();
        protected DataTable m_GridDataSource = new DataTable("AssetView");

        List<EUtility_Asset_XMissionStructure> m_DataSource;
        public List<EUtility_Asset_XMissionStructure> DataSource
        {
            set { m_DataSource = value; }
        }

        public DataGridView Datagrid
        {
            get { return grdAsset; }
        }

        public AssetGrid()
        {
            InitializeComponent();
            CreateTableHeader();
            grdAsset.DataSource = m_GridDataSource;
        }

        private void ReloadDataSource()
        {
            DataTable _DataTable = (DataTable)grdAsset.DataSource;
            object[] _DataRowValues;
            foreach (EUtility_Asset_XMissionStructure s in m_CurrentQueryWorker.Assets)
            {
                if (!m_AlredyStructuresLoad.Contains(s.Id))
                {
                    m_AlredyStructuresLoad.Add(s.Id);
                    _DataRowValues = new object[10];
                    _DataRowValues[0] = s.Id;
                    _DataRowValues[1] = s.Utility.Name; ;
                    _DataRowValues[2] = s.Project.Name;
                    _DataRowValues[3] = s.Longitude;
                    _DataRowValues[4] = s.Latitude;
                    _DataRowValues[5] = s.Altitude;
                    _DataRowValues[6] = s.SatelliteCount;
                    _DataRowValues[7] = s.SatelliteTime;
                    _DataRowValues[8] = s.UploadedImages;
                    _DataRowValues[9] = s.AllowEdit;
                    _DataTable.Rows.Add(_DataRowValues);
                }

            }
            m_AlredyStructuresLoad.Sort();
        }

        private void CreateTableHeader()
        {
            m_GridDataSource.Columns.Add(new DataColumn("ID", typeof(Int32)));
            m_GridDataSource.Columns.Add(new DataColumn("Asset Type", typeof(string)));
            m_GridDataSource.Columns.Add(new DataColumn("Project", typeof(string)));
            m_GridDataSource.Columns.Add(new DataColumn("Longitude", typeof(double)));
            m_GridDataSource.Columns.Add(new DataColumn("Latitude", typeof(double)));
            m_GridDataSource.Columns.Add(new DataColumn("Altitude", typeof(double)));
            m_GridDataSource.Columns.Add(new DataColumn("Satellite Count", typeof(int)));
            m_GridDataSource.Columns.Add(new DataColumn("Time", typeof(DateTime)));
            m_GridDataSource.Columns.Add(new DataColumn("Image Count", typeof(Int32)));
            m_GridDataSource.Columns.Add(new DataColumn("EditReady", typeof(bool)));
        }

        private void UpdateValue(string collumn, int id)
        {
            DataTable source = (DataTable)Datagrid.DataSource;
            foreach (DataRow dr in source.Rows)
            {
                if (ConvertHelper.Toint(dr["ID"]) == id)
                {
                    dr[collumn] = true;
                }
            }
        }

        public void UpdateValueAllowEdit(int id)
        {
            UpdateValue("EditReady", id);
        }

        public void ShowGrid()
        {
            //DataTable _DataTable = CreateDataTable();
            //grdAsset.DataSource = _DataTable;
        }

        public void ReloadAsyn()
        {
            foreach (EUtility_QueryAssetShown q in QueryAssetsToShown)
            {
                if (!q.IsShown && !q.IsBusy)
                {
                    if (!m_GridWorker.IsBusy)
                    {
                        m_CurrentQueryWorker = q;
                        m_GridWorker.RunWorkerAsync();
                    }
                }
            }

        }

        public void Reload()
        {
            if (!m_GridWorker.IsBusy)
            {
                m_AlredyStructuresLoad.Clear();
                CleanDataSource();
                foreach (EUtility_QueryAssetShown q in QueryAssetsToShown)
                {
                    if (q.IsShown && !q.IsBusy)
                    {
                        m_CurrentQueryWorker = q;
                        ReloadDataSource();
                    }
                }            
            }

        }

        private void CleanDataSource()
        {
            DataTable _DataTable = (DataTable)grdAsset.DataSource;
            _DataTable.Rows.Clear();
        }

        public void Clean()
        {
            CleanDataSource();
            QueryAssetsToShown.Clear();

        }

        public void DeselectRows()
        {
            foreach (DataGridViewRow r in Datagrid.SelectedRows)
            {
                r.Selected = false;
            }
        }

        #region Events

        public event EventHandler ClickedNew;
        public event EventHandler ClickedEdit;
        public event EventHandler ClickedDelete;
        public event EventHandler ClickedShowNear;
        public event EventHandler FinishLoadData;

        private void OnClickedShowNear(object o, EventArgs e)
        {
            // Safely invoke an event:
            if (ClickedShowNear != null)
            {
                ClickedShowNear(o, e);
            }
        }

        private void OnClickedNew(object o, EventArgs e)
        {
            // Safely invoke an event:
            if (ClickedNew != null)
            {
                ClickedNew(o, e);
            }
        }
        private void OnClickedEdit(object o, EventArgs e)
        {
            // Safely invoke an event:
            if (ClickedEdit != null)
            {
                ClickedEdit(o, e);
            }
        }
        private void OnClickedDelete(object o, EventArgs e)
        {
            // Safely invoke an event:
            if (ClickedDelete != null)
            {
                ClickedDelete(o, e);
            }
        }
        private void OnFinishLoadData(object o, EventArgs e)
        {
            // Safely invoke an event:
            if (FinishLoadData != null)
            {
                FinishLoadData(o, e);
            }
        }

        private void cmiNewAsset_Click(object sender, EventArgs e)
        {
            OnClickedNew(sender, e);
        }

        private void cmiEditAsset_Click(object sender, EventArgs e)
        {
            OnClickedEdit(sender, e);
        }

        private void cmiDeleteAsset_Click(object sender, EventArgs e)
        {
            OnClickedDelete(sender, e);
        }
        
        private void cmiShowNear_Click(object sender, EventArgs e)
        {
            ClickedShowNear(this, e);
        }

        private void grdAsset_DoubleClick(object sender, EventArgs e)
        {
            OnClickedEdit(sender, e);
        }
        #endregion Events

        #region Grid Worker Events
        private void m_GridWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            m_CurrentQueryWorker.IsBusy = true;
            m_CurrentQueryWorker.LoadAssets();
        }

        private void m_GridWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            //TODO
        }

        private void m_GridWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            m_CurrentQueryWorker.IsShown = true;
            m_CurrentQueryWorker.IsBusy = false;
            ReloadDataSource();
            OnFinishLoadData(sender, new EventArgs());
        }

        #endregion


    }
}
